package cc.rengu.igas.channel.wechat.biz.impl;

import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.facade.WechatPubService;
import cc.rengu.igas.channel.wechat.facade.request.*;
import cc.rengu.igas.channel.wechat.facade.response.*;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class WechatPubServiceImpl implements WechatPubService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<WechatGetAuthCodeResponse> getAuthCode(WechatGetAuthCodeRequest getAuthCodeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatGetAuthCodeResponse> result = new Result<>();
        WechatGetAuthCodeResponse getAuthCodeResponse = new WechatGetAuthCodeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(getAuthCodeRequest,
                    WechatServiceConstant.GET_AUTH_CODE_SERVICE, WechatGetAuthCodeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatGetAuthCodeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取用户授权失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getAuthCodeResponse.setRespDesc(((BizException) e).getCode());
                getAuthCodeResponse.setRespDesc(e.getMessage());
            } else {
                getAuthCodeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getAuthCodeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatGetOpenIdResponse> getOpenId(WechatGetOpenIdRequest getOpenIdRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatGetOpenIdResponse> result = new Result<>();
        WechatGetOpenIdResponse getOpenIdResponse = new WechatGetOpenIdResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(getOpenIdRequest,
                    WechatServiceConstant.GET_OPENID_SERVICE, WechatGetOpenIdResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatGetOpenIdResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取用户openId失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getOpenIdResponse.setRespDesc(((BizException) e).getCode());
                getOpenIdResponse.setRespDesc(e.getMessage());
            } else {
                getOpenIdResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getOpenIdResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatUploadImageResponse> uploadImage(WechatUploadImageRequest uploadImageRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatUploadImageResponse> result = new Result<>();
        WechatUploadImageResponse uploadImageResponse = new WechatUploadImageResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(uploadImageRequest,
                    WechatServiceConstant.UPLOAD_IMAGE_SERVICE, WechatUploadImageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatUploadImageResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("上传文件失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                uploadImageResponse.setRespDesc(((BizException) e).getCode());
                uploadImageResponse.setRespDesc(e.getMessage());
            } else {
                uploadImageResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                uploadImageResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NuccIdcQueryResponse> idcQuery(NuccIdcQueryRequest idcQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NuccIdcQueryResponse> result = new Result<>();
        NuccIdcQueryResponse idcQueryResponse = new NuccIdcQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(idcQueryRequest,
                    WechatServiceConstant.IDC_QUERY_SERVICE, NuccIdcQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NuccIdcQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("idc查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                idcQueryResponse.setRespDesc(((BizException) e).getCode());
                idcQueryResponse.setRespDesc(e.getMessage());
            } else {
                idcQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                idcQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GetJsapiTicketResponse> getJsapiTicket(GetJsapiTicketRequest getJsapiTicketRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GetJsapiTicketResponse> result = new Result<>();
        GetJsapiTicketResponse getJsapiTicketResponse = new GetJsapiTicketResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(getJsapiTicketRequest,
                    WechatServiceConstant.GET_TICKET_SERVICE, GetJsapiTicketResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GetJsapiTicketResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取ticket失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getJsapiTicketResponse.setRespDesc(((BizException) e).getCode());
                getJsapiTicketResponse.setRespDesc(e.getMessage());
            } else {
                getJsapiTicketResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getJsapiTicketResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

}
